<template>
  <view class="account-warpper">
    <scroll-view scroll-y class="order-list" @scrolltolower="loadMore" :lower-threshold="100">
      <Account :title="title" :incomeData="incomeData" :expensesData="expensesData" @typeChange="loadData"
        @clearData="clearData"></Account>
    </scroll-view>

    <!-- 加载状态提示 -->
    <u-loadmore :status="loadStatus" :loading-text="loadingText" :loadmore-text="loadmoreText" :nomore-text="nomoreText"
      marginTop="72rpx" color="#999" lineColor="#D9DCE2" line lineWidth="99rpx" />
  </view>
</template>

<script setup>
  import {
    ref
  } from 'vue'
  import Account from '@/components/Account.vue';
  import {
    onLoad
  } from '@dcloudio/uni-app'
  // 当前页码
  const page = ref(1)
  // 是否正在加载
  const loading = ref(false)
  // 加载状态提示
  const loadingText = ref('努力加载中');
  const loadmoreText = ref('滑动加载更多');
  const nomoreText = ref('实在没有了');
  // 是否还有更多数据
  const hasMore = ref(true)
  // 滚动区域高度
  const scrollHeight = ref(0)
  // 加载状态
  const loadStatus = ref('loadmore')


  const title = ref('')
  const index = ref(0)
  const incomeData = ref([])
  const expensesData = ref([])

  // 是否没有更多数据
  const noMore = ref(false)
  // 防抖计时器
  let debounceTimer = null
  onLoad((optiops) => {
    title.value = optiops.title
    index.value = optiops.index
    loadData(1)
	
	uni.setNavigationBarTitle({
		title: title.value
	});
  })
  const clearData = () => {
    incomeData.value = []
    expensesData.value = []
    page.value = 1
  }
  const loadData = async (page = 1, type = 1) => {
    if (loading.value || noMore.value) return

    // 防抖处理
    if (debounceTimer) {
      clearTimeout(debounceTimer)
    }

    debounceTimer = setTimeout(async () => {
      loading.value = true
      const name = ref('cashMoney')
      if (index.value == 1) {
        name.value = 'repeatMoney'
      } else if (index.value == 2) {
        name.value = 'interestMoney'
      } else if (index.value == 3) {
        name.value = 'scoreLog'
      }
      try {
		  if(page < 2){
			  uni.showLoading({
			  	title: '加载中……'
			  })
		  }
        await uni.$api.user[name.value]({
          type: type,
          page: page
        }).then(res => {
          if (res.data.data.length > 0) {
            if (type == 1) {
              incomeData.value = [...incomeData.value, ...res.data.data]
            } else {
              expensesData.value = [...expensesData.value, ...res.data.data]
            }
            hasMore.value = page < res.data.last_page;
            loadStatus.value = hasMore.value ? 'loadmore' : 'nomore'
          } else {
            noMore.value = true
          }
        })


      } catch (error) {
        console.error('加载数据失败:', error)
        uni.showToast({
          title: '加载失败',
          icon: 'none'
        })
      } finally {
        loading.value = false
        debounceTimer = null
        noMore.value = false
      }
    }, 300)
  }
  // 上拉加载更多
  const loadMore = () => {
    if (!loading.value && hasMore.value) {
      page.value++
      loadData(page.value)
    }
  }
</script>

<style lang="scss">
  .order-list {
    height: calc(100vh - 90px);
  }
</style>